package com.nlx.notes.module.service.impl.sms;

import com.nlx.notes.module.bean.dto.sms.SendSmsDTO;
import com.nlx.notes.module.bean.dto.sms.ThirdSmsSendDTO;
import com.nlx.notes.module.core.consts.RedisConst;
import com.nlx.notes.module.core.consts.SmsConst;
import com.nlx.notes.module.core.util.DataUtils;
import com.nlx.notes.module.core.util.UUIDUtils;
import com.nlx.notes.module.helper.sms.SmsSender;
import com.nlx.notes.module.service.sms.SmsManageService;
import com.nlx.notes.support.helper.redis.RedisHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class SmsManageServiceImpl implements SmsManageService {

    final SmsSender smsSender;
    final RedisHelper redisHelper;

    @Override
    public String sendSms(SendSmsDTO sendSmsDTO) {
        String code = DataUtils.generateCode(SmsConst.SMS_CODE_LENGTH);
        if(log.isDebugEnabled()){
            log.debug("发送短信验证码，手机号:{},验证码:{}", sendSmsDTO.getPhone(), code);
        }
        ThirdSmsSendDTO thirdSmsSendDTO = new ThirdSmsSendDTO();
        thirdSmsSendDTO.setPhone(sendSmsDTO.getPhone());
        thirdSmsSendDTO.setCode(code);
        boolean sendStatus = smsSender.sendSms(thirdSmsSendDTO);
        if(sendStatus){
            redisHelper.setValue(RedisConst.Token.USER_LOGIN_SMS_CODE_KEY+sendSmsDTO.getPhone(),code,
                    RedisConst.Token.USER_LOGIN_SMS_CODE_TIMEOUT,RedisConst.Token.USER_LOGIN_SMS_CODE_TIMEOUT_UNIT);
        }
        return UUIDUtils.genUUId();
    }
}
